import http from './http.js'
/**
 * 店铺实体类
 * 对应后端的Store模型
 */
class Store {
  constructor(data = {}) {
    this.store_id = data.store_id || ''
    this.name = data.name || ''
    this.address = data.address || ''
    this.phone = data.phone || ''
    this.description = data.description || ''
    this.image_url = data.image_url || ''
    this.store_type = data.store_type || ''
    this.open_time = data.open_time || ''
    this.close_time = data.close_time || ''
    this.hairdresser_commission_ratio = data.hairdresser_commission_ratio || 0.0
    this.washer_commission_ratio = data.washer_commission_ratio || 0.0
    this.mentorship_split_ratio = data.mentorship_split_ratio || 0.0
    this.created_at = data.created_at || ''
    this.updated_at = data.updated_at || ''
    this.is_deleted = data.is_deleted || false
    this.deleted_at = data.deleted_at || ''
    this.password_hash = data.password_hash || ''
    this.manager_id = data.manager_id || ''
  }
}

/**
 * 店铺相关接口封装
 * 对应后端的store_controller.py接口
 */

export default {
 
  /**
   * 创建新店铺
   * @param {Object} storeData - 店铺数据
   * @param {string} storeData.name - 店铺名称
   * @param {string} storeData.address - 店铺地址
   * @param {string} storeData.phone - 联系电话
   * @param {string} [storeData.description] - 店铺描述
   * @returns {Promise} 创建结果Promise
   */
  async createStore(storeData) {
    return http.post('/stores', storeData)
  },

  /**
   * 获取单个店铺信息
   * @param {string} storeId - 店铺ID
   * @returns {Promise} 店铺详细信息Promise
   */
  async getStoreById(storeId) {
    return http.get(`/stores/${storeId}`)
  },

  /**
   * 更新店铺信息
   * @param {string} storeId - 店铺ID
   * @param {Object} updateData - 更新数据
   * @returns {Promise} 更新结果Promise
   */
  async updateStore(storeId, updateData) {
    return http.put(`/stores/${storeId}`, updateData)
  },

  /**
   * 获取所有店铺列表
   * @param {Object} [query] - 查询参数
   * @param {number} [query.page=1] - 页码
   * @param {number} [query.limit=20] - 每页数量
   * @returns {Promise} 店铺列表Promise
   */
  async getStores(query = {}) {
    return http.get('/stores', { 
        ...query,
    })
  },

  /**
   * 绑定用户到店铺
   * @param {string} storeId - 店铺ID
   * @param {string} userId - 用户ID
   * @returns {Promise} 绑定结果Promise
   */
  async bindUserToStore(storeId, userId) {
    return http.post(`/stores/${storeId}/users/${userId}`)
  },

  /**
   * 通过手机号查询店铺
   * @param {string} phone - 手机号码
   * @returns {Promise} 店铺信息Promise
   */
  async getStoreByPhone(phone) {
    return http.get(`/stores/phone/${phone}`)
  },

  /**
   * 获取店铺下的所有用户(分页)
   * @param {string} storeId - 店铺ID
   * @param {Object} [params] - 分页参数
   * @param {number} [params.page=1] - 页码
   * @param {number} [params.limit=10] - 每页数量
   * @param {string} [params.name] - 用户姓名筛选
   * @returns {Promise} 用户列表Promise
   */
  async getStoreUsers(storeId, params = {}) {
    try {
      const response = await http.get(`/stores/${storeId}/users`, { ...params });
      return response.data;
    } catch (error) {
      console.error('获取卡包列表失败:', error);
      throw error;
    }
  }
}